package net.warl0ck.mjjp;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import net.warl0ck.mjjp.MJTable.MJPlace;
import net.warl0ck.mjjp.MJTypes.*;

public class MJPlayer extends JPanel implements ActionListener{
	private static final long serialVersionUID = 1L;
	private MJFile file;
	private MJTable table;
	private int cur_pos = 1;
	private int max_pos;
	private String locale;
	
	JLabel cntedt;
	Button first, prev, next, last;
	
	public MJPlayer(MJFile file, String loc, MJTable table){
        GridBagConstraints cons = new GridBagConstraints();
		cons.fill = GridBagConstraints.LINE_START;
		setLayout(new GridBagLayout());
		setBackground(new Color(0,64,0));
		setLoc(loc);

		cons.insets = new Insets(2,2,2,2);

		Button about = new Button(MJUtils.getLocaleStr("about", getLoc()));
		about.setForeground(Color.WHITE);
		about.setActionCommand("about");
		about.addActionListener(this);
		cons.gridx=0;	cons.gridy = 0;
		cons.anchor = GridBagConstraints.WEST;
		add(about,cons);

		Button info = new Button(MJUtils.getLocaleStr("info", getLoc()));
		info.setForeground(Color.WHITE);
		info.setActionCommand("info");
		info.addActionListener(this);
		cons.gridx=1;	cons.gridy = 0;
		cons.anchor = GridBagConstraints.WEST;
		add(info,cons);

		first = new Button(" |<");
		first.setForeground(Color.WHITE);
		first.setActionCommand("first");
		first.addActionListener(this);
		cons.gridx=2;	cons.gridy = 0;
		cons.anchor = GridBagConstraints.WEST;
		add(first,cons);

		prev = new Button("<<");
		prev.setForeground(Color.WHITE);
		prev.setActionCommand("prev");
		prev.addActionListener(this);
		cons.gridx=3;	cons.gridy = 0;
		cons.anchor = GridBagConstraints.WEST;
		add(prev,cons);

		next = new Button(">>");
		next.setForeground(Color.WHITE);
		next.setActionCommand("next");
		next.addActionListener(this);
		cons.gridx=4;	cons.gridy = 0;
		cons.anchor = GridBagConstraints.EAST;
		add(next,cons);

		last = new Button(">| ");
		last.setForeground(Color.WHITE);
		last.setActionCommand("last");
		last.addActionListener(this);
		cons.gridx=5;	cons.gridy = 0;
		cons.anchor = GridBagConstraints.EAST;
		add(last,cons);

		JLabel cntlbl = new JLabel(MJUtils.getLocaleStr("position",getLoc())+":");
		cntlbl.setForeground(Color.WHITE);
		cons.gridx=6;	cons.gridy = 0;
		cons.anchor = GridBagConstraints.EAST;
		add(cntlbl,cons);

		cntedt = new JLabel();
		cons.gridx=7;	cons.gridy = 0;
		cons.anchor = GridBagConstraints.WEST;
		cntedt.setForeground(Color.YELLOW);
		add(cntedt,cons);

		setFile(file);
		setTable(table);
		setMaxPos(getFile().getLog().size()+1);
		setCurPos(1);
	}

	public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("about")){
        	MJJPInfo info = new MJJPInfo(getLoc()); 
        	String about = info.name+"\n\n";
        	about += info.desc+"\n";
        	about += MJUtils.getLocaleStr("version",getLoc())+": "+info.version+",  "+MJUtils.getLocaleStr("date",getLoc())+": "+info.date+"\n\n";
        	about += info.text+"\n\n";
        	about += info.copy+"\n";
        	about += "Web: "+info.url+"\n";
        	about += "E-mail: "+info.mail+"\n";
        	about += "ICQ: "+info.icq;
    		JOptionPane.showMessageDialog(this.getParent(),about,MJUtils.getLocaleStr("about",getLoc()),JOptionPane.INFORMATION_MESSAGE);
        }
        if (e.getActionCommand().equals("info")){
        	String info;
        	if (getFile().getInfo().size() == 0){ info = MJUtils.getLocaleStr("noinfo", getLoc()); }else{ info = ""; }
        	for (String str: getFile().getInfo())
        		info += str+"\n";
    		JOptionPane.showMessageDialog(this.getParent(),info,MJUtils.getLocaleStr("info",getLoc()),JOptionPane.INFORMATION_MESSAGE);
        }
        if (e.getActionCommand().equals("next")){
           	setCurPos(getCurPos()+1);
        }
        if (e.getActionCommand().equals("prev")){
           	setCurPos(getCurPos()-1);
        }
        if (e.getActionCommand().equals("first")){
        	setCurPos(1);
        }
        if (e.getActionCommand().equals("last")){
        	setCurPos(getMaxPos());
        }
	}

	public void setCurPos(int cur_pos) {
		if (cur_pos>0 && cur_pos<=getMaxPos()){
			MJPlayFile pf = new MJPlayFile(getCurPos(), cur_pos);
    		if (cur_pos == 1){
    			prev.setEnabled(false);
    			first.setEnabled(false);
    			next.setEnabled(true);
    			last.setEnabled(true);
    		}else{
    			prev.setEnabled(true);
    			first.setEnabled(true);
    		}
    		if (cur_pos == getMaxPos()){
    			next.setEnabled(false);
    			last.setEnabled(false);
    			prev.setEnabled(true);
    			first.setEnabled(true);
    		}else{
    			next.setEnabled(true);
    			last.setEnabled(true);
    		}
    		this.cur_pos = cur_pos;
    		cntedt.setText(getCurPos()+" - "+getMaxPos());
    		playFile(pf);
    	}
	}

	public void playFile(MJPlayFile pf) {
		int i;
		setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
		if (pf.getFrom() != pf.getTo()){
			if (pf.getFrom()<pf.getTo()){
				// Forward
				for (i = pf.getFrom(); i<pf.getTo(); i++){
					clearTalks();
					MJLog act = file.getLogById(i);
					MJTail tail = new MJTail(act.getTail(),getLoc());
					getTable().setIndicator(act.getWind());
					if (act.getAction().equalsIgnoreCase("Move")){
						getTable().getPlaceByWind(act.getWind()).drawTalk(MJUtils.getLocaleStr("discard", getLoc())+" > "+tail.toString());
						getFile().getHandByWind(act.getWind()).discard(tail);
						getFile().getHandByWind(act.getWind()).sort();
						getTable().getPlaceByWind(act.getWind()).drawHand();
						getFile().getDiscardByWind(act.getWind()).add(tail);
						getTable().getDiscardPlaceByWind(act.getWind()).drawDiscard();
					}
					if (act.getAction().equalsIgnoreCase("Flower")){
						getTable().getPlaceByWind(act.getWind()).drawTalk(MJUtils.getLocaleStr("change", getLoc())+" > "+tail.toString());
						getFile().getHandByWind(act.getWind()).discard(tail);
						getFile().getHandByWind(act.getWind()).sort();
						getTable().getPlaceByWind(act.getWind()).drawHand();
						getFile().getFlowerByWind(act.getWind()).add(tail);
						getTable().getPlaceByWind(act.getWind()).drawFlower();
					}
					if (act.getAction().equalsIgnoreCase("Add")){
						getTable().getPlaceByWind(act.getWind()).drawTalk(MJUtils.getLocaleStr("fromwall", getLoc())+" > "+tail.toString());
						getFile().getHandByWind(act.getWind()).add(tail);
						getFile().getHandByWind(act.getWind()).sort();
						getTable().getPlaceByWind(act.getWind()).drawHand();
					}
					if (act.getAction().equalsIgnoreCase("Pung")){
						getTable().getPlaceByWind(act.getWind()).drawTalk(MJUtils.getLocaleStr("pung", getLoc())+" > "+tail.toString());
						getFile().getDiscardByWind(act.getAdv()).discard(tail);
						getTable().getDiscardPlaceByWind(act.getAdv()).drawDiscard();
						getFile().getHandByWind(act.getWind()).discard(tail);
						getFile().getHandByWind(act.getWind()).discard(tail);
						getFile().getHandByWind(act.getWind()).sort();
						getTable().getPlaceByWind(act.getWind()).drawHand();
						getFile().getOpenByWind(act.getWind()).add(tail);
						getFile().getOpenByWind(act.getWind()).add(tail);
						getFile().getOpenByWind(act.getWind()).add(tail);
						getTable().getPlaceByWind(act.getWind()).drawOpen();
					}
					if (act.getAction().equalsIgnoreCase("ChowU")){
						String[] numbs = {(Integer.parseInt(tail.get_num())-2)+"",(Integer.parseInt(tail.get_num())-1)+""};
						String type = tail.get_type();
						MJTail tail1 = new MJTail(numbs[0], type, false, false, getLoc());
						MJTail tail2 = new MJTail(numbs[1], type, false, false, getLoc());
						getTable().getPlaceByWind(act.getWind()).drawTalk(MJUtils.getLocaleStr("chow", getLoc())+" > "+tail.toString());
						getFile().getDiscardByWind(act.getAdv()).discard(tail);
						getTable().getDiscardPlaceByWind(act.getAdv()).drawDiscard();
						getFile().getHandByWind(act.getWind()).discard(tail1);
						getFile().getHandByWind(act.getWind()).discard(tail2);
						getFile().getHandByWind(act.getWind()).sort();
						getTable().getPlaceByWind(act.getWind()).drawHand();
						getFile().getOpenByWind(act.getWind()).add(tail1);
						getFile().getOpenByWind(act.getWind()).add(tail2);
						getFile().getOpenByWind(act.getWind()).add(tail);
						getTable().getPlaceByWind(act.getWind()).drawOpen();
					}
					if (act.getAction().equalsIgnoreCase("ChowL")){
						String[] numbs = {(Integer.parseInt(tail.get_num())+1)+"",(Integer.parseInt(tail.get_num())+2)+""};
						String type = tail.get_type();
						MJTail tail1 = new MJTail(numbs[0], type, false, false, getLoc());
						MJTail tail2 = new MJTail(numbs[1], type, false, false, getLoc());
						getTable().getPlaceByWind(act.getWind()).drawTalk(MJUtils.getLocaleStr("chow", getLoc())+" > "+tail.toString());
						getFile().getDiscardByWind(act.getAdv()).discard(tail);
						getTable().getDiscardPlaceByWind(act.getAdv()).drawDiscard();
						getFile().getHandByWind(act.getWind()).discard(tail1);
						getFile().getHandByWind(act.getWind()).discard(tail2);
						getFile().getHandByWind(act.getWind()).sort();
						getTable().getPlaceByWind(act.getWind()).drawHand();
						getFile().getOpenByWind(act.getWind()).add(tail);
						getFile().getOpenByWind(act.getWind()).add(tail1);
						getFile().getOpenByWind(act.getWind()).add(tail2);
						getTable().getPlaceByWind(act.getWind()).drawOpen();
					}
					if (act.getAction().equalsIgnoreCase("ChowM")){
						String[] numbs = {(Integer.parseInt(tail.get_num())-1)+"",(Integer.parseInt(tail.get_num())+1)+""};
						String type = tail.get_type();
						MJTail tail1 = new MJTail(numbs[0], type, false, false, getLoc());
						MJTail tail2 = new MJTail(numbs[1], type, false, false, getLoc());
						getTable().getPlaceByWind(act.getWind()).drawTalk(MJUtils.getLocaleStr("chow", getLoc())+" > "+tail.toString());
						getFile().getDiscardByWind(act.getAdv()).discard(tail);
						getTable().getDiscardPlaceByWind(act.getAdv()).drawDiscard();
						getFile().getHandByWind(act.getWind()).discard(tail1);
						getFile().getHandByWind(act.getWind()).discard(tail2);
						getFile().getHandByWind(act.getWind()).sort();
						getTable().getPlaceByWind(act.getWind()).drawHand();
						getFile().getOpenByWind(act.getWind()).add(tail1);
						getFile().getOpenByWind(act.getWind()).add(tail);
						getFile().getOpenByWind(act.getWind()).add(tail2);
						getTable().getPlaceByWind(act.getWind()).drawOpen();
					}
					if (act.getAction().equalsIgnoreCase("KongO")){
						getTable().getPlaceByWind(act.getWind()).drawTalk(MJUtils.getLocaleStr("kongo", getLoc())+" > "+tail.toString());
						getFile().getDiscardByWind(act.getAdv()).discard(tail);
						getTable().getDiscardPlaceByWind(act.getAdv()).drawDiscard();
						getFile().getHandByWind(act.getWind()).discard(tail);
						getFile().getHandByWind(act.getWind()).discard(tail);
						getFile().getHandByWind(act.getWind()).discard(tail);
						getFile().getHandByWind(act.getWind()).sort();
						getTable().getPlaceByWind(act.getWind()).drawHand();
						getFile().getOpenByWind(act.getWind()).add(tail);
						getFile().getOpenByWind(act.getWind()).add(tail);
						getFile().getOpenByWind(act.getWind()).add(tail);
						getFile().getOpenByWind(act.getWind()).add(tail);
						getTable().getPlaceByWind(act.getWind()).drawOpen();
					}
					if (act.getAction().equalsIgnoreCase("KongC")){
						getTable().getPlaceByWind(act.getWind()).drawTalk(MJUtils.getLocaleStr("kongc", getLoc())+" > "+tail.toString());
						getFile().getHandByWind(act.getWind()).discard(tail);
						getFile().getHandByWind(act.getWind()).discard(tail);
						getFile().getHandByWind(act.getWind()).discard(tail);
						getFile().getHandByWind(act.getWind()).discard(tail);
						getFile().getHandByWind(act.getWind()).sort();
						getTable().getPlaceByWind(act.getWind()).drawHand();
						getFile().getOpenByWind(act.getWind()).add(new MJTail("xx",getLoc()));
						getFile().getOpenByWind(act.getWind()).add(tail);
						getFile().getOpenByWind(act.getWind()).add(tail);
						getFile().getOpenByWind(act.getWind()).add(new MJTail("xx",getLoc()));
						getTable().getPlaceByWind(act.getWind()).drawOpen();
					}
					if (act.getAction().equalsIgnoreCase("Mahjong")){
						getTable().getPlaceByWind(act.getWind()).drawTalk(MJUtils.getLocaleStr("mahjong", getLoc())+" > "+tail.toString());
						if (! act.getWind().equalsIgnoreCase(act.getAdv())){
							getFile().getDiscardByWind(act.getAdv()).discard(tail);
							getTable().getDiscardPlaceByWind(act.getAdv()).drawDiscard();
							if (act.getSuit().equalsIgnoreCase("Pair")){ // Mahjong pair
								getFile().getOpenByWind(act.getWind()).add(tail);
								getFile().getOpenByWind(act.getWind()).add(tail);
								getTable().getPlaceByWind(act.getWind()).drawOpen();
							}
							if (act.getSuit().equalsIgnoreCase("Pung")){ // Mahjong pung
								getFile().getOpenByWind(act.getWind()).add(tail);
								getFile().getOpenByWind(act.getWind()).add(tail);
								getFile().getOpenByWind(act.getWind()).add(tail);
								getTable().getPlaceByWind(act.getWind()).drawOpen();
								
							}
							if (act.getSuit().equalsIgnoreCase("ChowU")){ // Mahjong upper chow
								String[] numbs = {(Integer.parseInt(tail.get_num())-2)+"",(Integer.parseInt(tail.get_num())-1)+""};
								String type = tail.get_type();
								MJTail tail1 = new MJTail(numbs[0], type, false, false, getLoc());
								MJTail tail2 = new MJTail(numbs[1], type, false, false, getLoc());
								getFile().getOpenByWind(act.getWind()).add(tail1);
								getFile().getOpenByWind(act.getWind()).add(tail2);
								getFile().getOpenByWind(act.getWind()).add(tail);
								getTable().getPlaceByWind(act.getWind()).drawOpen();
							}
							if (act.getSuit().equalsIgnoreCase("ChowL")){ // Mahjong lower chow
								String[] numbs = {(Integer.parseInt(tail.get_num())+1)+"",(Integer.parseInt(tail.get_num())+2)+""};
								String type = tail.get_type();
								MJTail tail1 = new MJTail(numbs[0], type, false, false, getLoc());
								MJTail tail2 = new MJTail(numbs[1], type, false, false, getLoc());
								getFile().getOpenByWind(act.getWind()).add(tail);
								getFile().getOpenByWind(act.getWind()).add(tail1);
								getFile().getOpenByWind(act.getWind()).add(tail2);
								getTable().getPlaceByWind(act.getWind()).drawOpen();
							}
							if (act.getSuit().equalsIgnoreCase("ChowM")){ // Mahjong middle chow
								String[] numbs = {(Integer.parseInt(tail.get_num())-1)+"",(Integer.parseInt(tail.get_num())+1)+""};
								String type = tail.get_type();
								MJTail tail1 = new MJTail(numbs[0], type, false, false, getLoc());
								MJTail tail2 = new MJTail(numbs[1], type, false, false, getLoc());
								getFile().getOpenByWind(act.getWind()).add(tail1);
								getFile().getOpenByWind(act.getWind()).add(tail);
								getFile().getOpenByWind(act.getWind()).add(tail2);
								getTable().getPlaceByWind(act.getWind()).drawOpen();
							}
						}
						MJHand h = new MJHand(getLoc());
						if (act.getHand() != null && ! act.getHand().isEmpty()){
							String[] tails_hand = act.getHand().split("\\s+");
							for (String tl: tails_hand)
								h.add(new MJTail(tl,getLoc()));
						}
						getFile().getHandByWind(act.getWind()).setHand(h.getHand());
						getFile().getHandByWind(act.getWind()).sort();
						getTable().getPlaceByWind(act.getWind()).drawHand();
					}
				}
			}else{
				// Reward
				for (i = pf.getFrom(); i>pf.getTo(); i--){
					clearTalks();
					MJLog act = file.getLogById(i-1);
					MJTail tail = new MJTail(act.getTail(),getLoc());
					getTable().setIndicator(act.getWind());
					if (act.getAction().equalsIgnoreCase("Move")){
						getTable().getPlaceByWind(act.getWind()).drawTalk(MJUtils.getLocaleStr("discard", getLoc())+" > "+tail.toString());
						getFile().getDiscardByWind(act.getWind()).discard(tail);
						getTable().getDiscardPlaceByWind(act.getWind()).drawDiscard();
						if (i>2	&& getFile().getHandByWind(act.getWind()).is_closed())
							tail = new MJTail("ii",getLoc());
						getFile().getHandByWind(act.getWind()).add(tail);
						getFile().getHandByWind(act.getWind()).sort();
						getTable().getPlaceByWind(act.getWind()).drawHand();
					}
					if (act.getAction().equalsIgnoreCase("Flower")){
						getTable().getPlaceByWind(act.getWind()).drawTalk(MJUtils.getLocaleStr("change", getLoc())+" > "+tail.toString());
						getFile().getFlowerByWind(act.getWind()).discard(tail);
						getTable().getPlaceByWind(act.getWind()).drawFlower();
						if ((i==2) || (i>2	&& getFile().getHandByWind(act.getWind()).is_closed()))
								tail = new MJTail("ii",getLoc());
						getFile().getHandByWind(act.getWind()).add(tail);
						getFile().getHandByWind(act.getWind()).sort();
						getTable().getPlaceByWind(act.getWind()).drawHand();
					}
					if (act.getAction().equalsIgnoreCase("Add")){
						getTable().getPlaceByWind(act.getWind()).drawTalk(MJUtils.getLocaleStr("fromwall", getLoc())+" > "+tail.toString());
						getFile().getHandByWind(act.getWind()).discard(tail);
						getFile().getHandByWind(act.getWind()).sort();
						getTable().getPlaceByWind(act.getWind()).drawHand();
					}
					if (act.getAction().equalsIgnoreCase("Pung")){
						getTable().getPlaceByWind(act.getWind()).drawTalk(MJUtils.getLocaleStr("pung", getLoc())+" > "+tail.toString());
						getFile().getOpenByWind(act.getWind()).discard(tail);
						getFile().getOpenByWind(act.getWind()).discard(tail);
						getFile().getOpenByWind(act.getWind()).discard(tail);
						getTable().getPlaceByWind(act.getWind()).drawOpen();
						getFile().getDiscardByWind(act.getAdv()).add(tail);
						getTable().getDiscardPlaceByWind(act.getAdv()).drawDiscard();
						if (i>2	&& getFile().getHandByWind(act.getWind()).is_closed())
								tail = new MJTail("ii",getLoc());
						getFile().getHandByWind(act.getWind()).add(tail);
						getFile().getHandByWind(act.getWind()).add(tail);
						getFile().getHandByWind(act.getWind()).sort();
						getTable().getPlaceByWind(act.getWind()).drawHand();
					}
					if (act.getAction().equalsIgnoreCase("ChowU")){
						String[] numbs = {(Integer.parseInt(tail.get_num())-2)+"",(Integer.parseInt(tail.get_num())-1)+""};
						String type = tail.get_type();
						MJTail tail1 = new MJTail(numbs[0], type, false, false, getLoc());
						MJTail tail2 = new MJTail(numbs[1], type, false, false, getLoc());
						getTable().getPlaceByWind(act.getWind()).drawTalk(MJUtils.getLocaleStr("chow", getLoc())+" > "+tail.toString());
						getFile().getOpenByWind(act.getWind()).discard(tail1);
						getFile().getOpenByWind(act.getWind()).discard(tail2);
						getFile().getOpenByWind(act.getWind()).discard(tail);
						getTable().getPlaceByWind(act.getWind()).drawOpen();
						getFile().getDiscardByWind(act.getAdv()).add(tail);
						getTable().getDiscardPlaceByWind(act.getAdv()).drawDiscard();
						if (i>2	&& getFile().getHandByWind(act.getWind()).is_closed()){
							tail1 = new MJTail("ii",getLoc());
							tail2 = new MJTail("ii",getLoc());
						}
						getFile().getHandByWind(act.getWind()).add(tail1);
						getFile().getHandByWind(act.getWind()).add(tail2);
						getFile().getHandByWind(act.getWind()).sort();
						getTable().getPlaceByWind(act.getWind()).drawHand();
					}
					if (act.getAction().equalsIgnoreCase("ChowL")){
						String[] numbs = {(Integer.parseInt(tail.get_num())+1)+"",(Integer.parseInt(tail.get_num())+2)+""};
						String type = tail.get_type();
						MJTail tail1 = new MJTail(numbs[0], type, false, false, getLoc());
						MJTail tail2 = new MJTail(numbs[1], type, false, false, getLoc());
						getTable().getPlaceByWind(act.getWind()).drawTalk(MJUtils.getLocaleStr("chow", getLoc())+" > "+tail.toString());
						getFile().getOpenByWind(act.getWind()).discard(tail);
						getFile().getOpenByWind(act.getWind()).discard(tail1);
						getFile().getOpenByWind(act.getWind()).discard(tail2);
						getTable().getPlaceByWind(act.getWind()).drawOpen();
						getFile().getDiscardByWind(act.getAdv()).add(tail);
						getTable().getDiscardPlaceByWind(act.getAdv()).drawDiscard();
						if (i>2	&& getFile().getHandByWind(act.getWind()).is_closed()){
							tail1 = new MJTail("ii",getLoc());
							tail2 = new MJTail("ii",getLoc());
						}
						getFile().getHandByWind(act.getWind()).add(tail1);
						getFile().getHandByWind(act.getWind()).add(tail2);
						getFile().getHandByWind(act.getWind()).sort();
						getTable().getPlaceByWind(act.getWind()).drawHand();
					}
					if (act.getAction().equalsIgnoreCase("ChowM")){
						String[] numbs = {(Integer.parseInt(tail.get_num())-1)+"",(Integer.parseInt(tail.get_num())+1)+""};
						String type = tail.get_type();
						MJTail tail1 = new MJTail(numbs[0], type, false, false, getLoc());
						MJTail tail2 = new MJTail(numbs[1], type, false, false, getLoc());
						getTable().getPlaceByWind(act.getWind()).drawTalk(MJUtils.getLocaleStr("chow", getLoc())+" > "+tail.toString());
						getFile().getOpenByWind(act.getWind()).discard(tail1);
						getFile().getOpenByWind(act.getWind()).discard(tail);
						getFile().getOpenByWind(act.getWind()).discard(tail2);
						getTable().getPlaceByWind(act.getWind()).drawOpen();
						getFile().getDiscardByWind(act.getAdv()).add(tail);
						getTable().getDiscardPlaceByWind(act.getAdv()).drawDiscard();
						if (i>2	&& getFile().getHandByWind(act.getWind()).is_closed()){
							tail1 = new MJTail("ii",getLoc());
							tail2 = new MJTail("ii",getLoc());
						}
						getFile().getHandByWind(act.getWind()).add(tail1);
						getFile().getHandByWind(act.getWind()).add(tail2);
						getFile().getHandByWind(act.getWind()).sort();
						getTable().getPlaceByWind(act.getWind()).drawHand();
					}
					if (act.getAction().equalsIgnoreCase("KongO")){
						getTable().getPlaceByWind(act.getWind()).drawTalk(MJUtils.getLocaleStr("kongo", getLoc())+" > "+tail.toString());
						getFile().getOpenByWind(act.getWind()).discard(tail);
						getFile().getOpenByWind(act.getWind()).discard(tail);
						getFile().getOpenByWind(act.getWind()).discard(tail);
						getFile().getOpenByWind(act.getWind()).discard(tail);
						getTable().getPlaceByWind(act.getWind()).drawOpen();
						getFile().getDiscardByWind(act.getAdv()).add(tail);
						getTable().getDiscardPlaceByWind(act.getAdv()).drawDiscard();
						if (i>2	&& getFile().getHandByWind(act.getWind()).is_closed())
								tail = new MJTail("ii",getLoc());
						getFile().getHandByWind(act.getWind()).add(new MJTail(tail));
						getFile().getHandByWind(act.getWind()).add(new MJTail(tail));
						getFile().getHandByWind(act.getWind()).add(new MJTail(tail));
						getFile().getHandByWind(act.getWind()).sort();
						getTable().getPlaceByWind(act.getWind()).drawHand();
					}
					if (act.getAction().equalsIgnoreCase("KongC")){
						getTable().getPlaceByWind(act.getWind()).drawTalk(MJUtils.getLocaleStr("kongc", getLoc())+" > "+tail.toString());
						getFile().getOpenByWind(act.getWind()).discard(new MJTail("xx",getLoc()));
						getFile().getOpenByWind(act.getWind()).discard(tail);
						getFile().getOpenByWind(act.getWind()).discard(tail);
						getFile().getOpenByWind(act.getWind()).discard(new MJTail("xx",getLoc()));
						getTable().getPlaceByWind(act.getWind()).drawOpen();
						getFile().getHandByWind(act.getWind()).add(tail);
						getFile().getHandByWind(act.getWind()).add(new MJTail(tail));
						getFile().getHandByWind(act.getWind()).add(new MJTail(tail));
						getFile().getHandByWind(act.getWind()).add(new MJTail(tail));
						getFile().getHandByWind(act.getWind()).sort();
						getTable().getPlaceByWind(act.getWind()).drawHand();
					}
					if (act.getAction().equalsIgnoreCase("Mahjong")){
						getTable().getPlaceByWind(act.getWind()).drawTalk(MJUtils.getLocaleStr("mahjong", getLoc())+" > "+tail.toString());
						if (! act.getWind().equalsIgnoreCase(act.getAdv())){
							getFile().getDiscardByWind(act.getAdv()).add(tail);
							getTable().getDiscardPlaceByWind(act.getAdv()).drawDiscard();
							if (act.getSuit().equalsIgnoreCase("Pair")){
								getFile().getOpenByWind(act.getWind()).discard(tail);
								getFile().getOpenByWind(act.getWind()).discard(tail);
								getTable().getPlaceByWind(act.getWind()).drawOpen();
							}
							if (act.getSuit().equalsIgnoreCase("Pung")){
								getFile().getOpenByWind(act.getWind()).discard(tail);
								getFile().getOpenByWind(act.getWind()).discard(tail);
								getFile().getOpenByWind(act.getWind()).discard(tail);
								getTable().getPlaceByWind(act.getWind()).drawOpen();
								if (act.isClosed())
									tail = new MJTail("ii",getLoc());
								getFile().getHandByWind(act.getWind()).add(tail);
								getFile().getHandByWind(act.getWind()).add(tail);
								getFile().getHandByWind(act.getWind()).sort();
								getTable().getPlaceByWind(act.getWind()).drawHand();
							}
							if (act.getSuit().equalsIgnoreCase("ChowU")){
								String[] numbs = {(Integer.parseInt(tail.get_num())-2)+"",(Integer.parseInt(tail.get_num())-1)+""};
								String type = tail.get_type();
								MJTail tail1 = new MJTail(numbs[0], type, false, false, getLoc());
								MJTail tail2 = new MJTail(numbs[1], type, false, false, getLoc());
								getFile().getOpenByWind(act.getWind()).discard(tail1);
								getFile().getOpenByWind(act.getWind()).discard(tail2);
								getFile().getOpenByWind(act.getWind()).discard(tail);
								getTable().getPlaceByWind(act.getWind()).drawOpen();
								if (act.isClosed()){
									tail1 = new MJTail("ii",getLoc());
									tail2 = new MJTail("ii",getLoc());
								}
								getFile().getHandByWind(act.getWind()).add(tail1);
								getFile().getHandByWind(act.getWind()).add(tail2);
								getFile().getHandByWind(act.getWind()).sort();
								getTable().getPlaceByWind(act.getWind()).drawHand();
							}
							if (act.getSuit().equalsIgnoreCase("ChowL")){
								String[] numbs = {(Integer.parseInt(tail.get_num())+1)+"",(Integer.parseInt(tail.get_num())+2)+""};
								String type = tail.get_type();
								MJTail tail1 = new MJTail(numbs[0], type, false, false, getLoc());
								MJTail tail2 = new MJTail(numbs[1], type, false, false, getLoc());
								getFile().getOpenByWind(act.getWind()).discard(tail);
								getFile().getOpenByWind(act.getWind()).discard(tail1);
								getFile().getOpenByWind(act.getWind()).discard(tail2);
								getTable().getPlaceByWind(act.getWind()).drawOpen();
								if (act.isClosed()){
									tail1 = new MJTail("ii",getLoc());
									tail2 = new MJTail("ii",getLoc());
								}
								getFile().getHandByWind(act.getWind()).add(tail1);
								getFile().getHandByWind(act.getWind()).add(tail2);
								getFile().getHandByWind(act.getWind()).sort();
								getTable().getPlaceByWind(act.getWind()).drawHand();
							}
							if (act.getSuit().equalsIgnoreCase("ChowM")){
								String[] numbs = {(Integer.parseInt(tail.get_num())-1)+"",(Integer.parseInt(tail.get_num())+1)+""};
								String type = tail.get_type();
								MJTail tail1 = new MJTail(numbs[0], type, false, false, getLoc());
								MJTail tail2 = new MJTail(numbs[1], type, false, false, getLoc());
								getFile().getOpenByWind(act.getWind()).discard(tail1);
								getFile().getOpenByWind(act.getWind()).discard(tail);
								getFile().getOpenByWind(act.getWind()).discard(tail2);
								getTable().getPlaceByWind(act.getWind()).drawOpen();
								if (act.isClosed()){
									tail1 = new MJTail("ii",getLoc());
									tail2 = new MJTail("ii",getLoc());
								}
								getFile().getHandByWind(act.getWind()).add(tail1);
								getFile().getHandByWind(act.getWind()).add(tail2);
								getFile().getHandByWind(act.getWind()).sort();
								getTable().getPlaceByWind(act.getWind()).drawHand();
							}
						}
						MJHand h = new MJHand(getLoc());
						if (act.getHand() != null && ! act.getHand().isEmpty()){
							String[] tails_hand = act.getHand().split("\\s+");
							for (String tl: tails_hand)
								if (act.isClosed())
									h.add(new MJTail("ii",getLoc()));
								else
									h.add(new MJTail(tl,getLoc()));
						}
						getFile().getHandByWind(act.getWind()).setHand(h.getHand());
						getFile().getHandByWind(act.getWind()).sort();
						getTable().getPlaceByWind(act.getWind()).drawHand();
					}
				}
			}
		}
		setCursor(Cursor.getDefaultCursor());
	}

	public int getCurPos() {
		return cur_pos;
	}

	public void setMaxPos(int max_pos) {
		this.max_pos = max_pos;
	}

	public int getMaxPos() {
		return max_pos;
	}

	public void setTable(MJTable table) {
		this.table = table;
	}

	public MJTable getTable() {
		return table;
	}
	public void setFile(MJFile file) {
		this.file = file;
	}

	public MJFile getFile() {
		return file;
	}
	public void clearTalks(){
		for (MJPlace p: getTable().getPlaces())
			p.drawTalk("");
	}

	public void setLoc(String locale) {
		this.locale = locale;
	}

	public String getLoc() {
		return locale;
	}
}
